package Control;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.*;
import java.util.ArrayList;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

import Database.*;
import Model.*;

public class EditUser extends JFrame implements ActionListener
{
        private JLabel id, jlStaff, jlUsername, jlFirst, jlLast, jlPass,
                        logoPicture;
        private JTextField jtID, jtUsername, jtFirst, jtLast;
        private JPasswordField jtPass;
        private JButton search, save, cancel, clear;
        private JPanel logo, leftPanel, rightPanel;
        private JCheckBox manager;
        private TableOperations to;
        private JTable jt;
        private DefaultTableModel jtM;
        private ArrayList users;
        private ArrayList<User> test = new ArrayList<User>();
        private User toBeEdited = null;
        private boolean isManager;
        private ImageIcon icon = new ImageIcon("frameIcon.png");

        public EditUser(TableOperations to)
        {
                GridBagLayout layout = new GridBagLayout();
                GridBagConstraints gc = new GridBagConstraints();

                setIconImage(icon.getImage());
                setTitle("MDL - Edit User");
                setSize(750, 300);
                setLocationRelativeTo(null);
                setResizable(false);
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                this.setLayout(new GridLayout(1, 2));

                this.to = to;
                to.queryUsers();
                Insets insets = new Insets(15, 15, 13, 3);

                // leftPanel
                leftPanel = new JPanel();
                leftPanel.setLayout(layout);

                id = new JLabel("ID or Name");
                gc.gridx = 0;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(id, gc);

                jtID = new JTextField(10);
                gc.gridx = 0;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, 100, 13, 15);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(jtID, gc);

                search = new JButton("Search");
                search.addActionListener(this);
                gc.gridx = 0;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(0, 150, 0, 125);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(search, gc);

                clear = new JButton("Clear");
                clear.addActionListener(this);
                gc.gridx = 0;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(0, 250, 0, 25);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(clear, gc);

                jlStaff = new JLabel("Staff");
                gc.gridx = 0;
                gc.gridy = 2;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(0, 50, 0, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(jlStaff, gc);

                // Users Table
                createTable();
                gc.gridx = 0;
                gc.gridy = 3;
                gc.weighty = 1;
                gc.weightx = 3;
                insets = new Insets(5, 25, 15, 30);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(new JScrollPane(jt), gc);

                add(leftPanel);

                insets = new Insets(15, 15, 5, 3);
                // Right Panel
                rightPanel = new JPanel();
                rightPanel.setLayout(layout);

                jlUsername = new JLabel("Username");
                gc.gridx = 0;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 75, 5, 30);
                gc.insets = insets;
                rightPanel.add(jlUsername, gc);

                jtUsername = new JTextField();
                gc.gridx = 2;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -125, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(jtUsername, gc);

                jlFirst = new JLabel("First name");
                gc.gridx = 0;
                gc.gridy = 3;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 75, 5, 30);
                gc.insets = insets;
                rightPanel.add(jlFirst, gc);

                jtFirst = new JTextField();
                gc.gridx = 2;
                gc.gridy = 3;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -125, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(jtFirst, gc);

                jlLast = new JLabel("Last Name");
                gc.gridx = 0;
                gc.gridy = 5;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 75, 5, 30);
                gc.insets = insets;
                rightPanel.add(jlLast, gc);

                jtLast = new JTextField();
                gc.gridx = 2;
                gc.gridy = 5;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -125, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(jtLast, gc);

                jlPass = new JLabel("Password");
                gc.gridx = 0;
                gc.gridy = 7;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 75, 5, 30);
                gc.insets = insets;
                rightPanel.add(jlPass, gc);

                jtPass = new JPasswordField();
                gc.gridx = 2;
                gc.gridy = 7;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -125, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(jtPass, gc);

                gc.gridx = 0;
                gc.gridy = 8;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 75, 5, 30);
                gc.insets = insets;
                rightPanel.add(new JLabel("Manager"), gc);

                manager = new JCheckBox();
                gc.gridx = 2;
                gc.gridy = 8;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -125, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(manager, gc);

                save = new JButton("Save Changes");
                save.addActionListener(this);
                gc.gridx = 0;
                gc.gridy = 9;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, 50, 5, 75);
                gc.insets = insets;
                rightPanel.add(save, gc);

                cancel = new JButton("Cancel");
                cancel.addActionListener(this);
                gc.gridx = 2;
                gc.gridy = 9;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -65, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(cancel, gc);

                gc.gridx = 3;
                gc.gridy = 2;
                gc.weighty = 1;
                gc.weightx = 1;
                this.add(rightPanel, gc);

        }

        @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent e)
        {
                if (e.getSource() == cancel)
                        this.dispose();
                else if (e.getSource() == search)
                {
                        if (jtM.getRowCount() > 0)
                        {
                                clear();
                                search();
                        } else
                                search();
                } else if (e.getSource() == clear)
                        clear();
                else if (e.getSource() == save)
                {
                        if (jtUsername.getText().equals("") || jtFirst.getText().equals("") || jtLast.getText().equals("") || jtPass.getText().equals(""))
                                JOptionPane.showMessageDialog(null, "Check Blank Fields");
                        else
                        {
                                toBeEdited.setUsername(jtUsername.getText());
                                toBeEdited.setFname(jtFirst.getText());
                                toBeEdited.setLname(jtLast.getText());
                                toBeEdited.setPassword(jtPass.getText());

                                if (to.updateUser(toBeEdited))
                                {
                                        if (isManager == true && manager.isSelected() == false)
                                        {
                                                to.becomeSA(toBeEdited);
                                        } else if (isManager == false
                                                        && manager.isSelected() == true)
                                        {
                                                to.becomeManager(toBeEdited);
                                        } else if (isManager == true
                                                        && manager.isSelected() == true)
                                                ;
                                        else if (isManager == false
                                                        && manager.isSelected() == false)
                                                ;
                                        JOptionPane.showMessageDialog(null, "User updated");
                                        clear();
                                } else
                                        JOptionPane.showMessageDialog(null, "Update Unsuccessful");
                        }
                }
        }

        public void createTable()
        {
                jtM = new DefaultTableModel();

                jtM.addColumn("First Name");
                jtM.addColumn("Last Name");

                fillTable();

                jt = new JTable(jtM);
                jt.addMouseListener(new MouseAdapter()
                {
                        public void mousePressed(MouseEvent e)
                        {
                                int row = jt.getSelectedRow();
                                int userID = test.get(row).getStaffID();
                                String username = test.get(row).getUsername();
                                String fname, lname;
                                if(test.get(row).getFname().isEmpty() || test.get(row).getLname().isEmpty())
                                {
                                        fname = test.get(row).getFname();
                                        lname = test.get(row).getLname();
                                }
                                else
                                {
                                        fname = test.get(row).getFname().trim();
                                        lname = test.get(row).getLname().trim();
                                }
                                String password = test.get(row).getPassword();
                                toBeEdited = new User(userID, username, fname, lname, password);
                                jtUsername.setText(username);
                                jtFirst.setText(fname);
                                jtLast.setText(lname);
                                jtPass.setText(password);
                                isManager = to.isManagerOrSA(username);
                                if(isManager == true)
                                        manager.setSelected(true);
                                else
                                        manager.setSelected(false);                             
                        }
                });
        }

        public void clear()
        {
                try
                {
                        jtM.removeRow(0);
                        for (int i = 0; i < jtM.getRowCount(); i++)
                        {
                                jtM.removeRow(i);
                        }

                        test.remove(0);
                        for (int i = 0; i < test.size(); i++)
                        {
                                test.remove(i);
                        }
                } catch (Exception e)
                {
                        System.out.println("No rows to be deleted");
                } finally
                {
                        // jtID.setText("");
                        jtUsername.setText("");
                        jtFirst.setText("");
                        jtLast.setText("");
                        jtPass.setText("");
                        manager.setSelected(false);
                }
        }

        public void fillTable()
        {
                if (jtM.getRowCount() >= 1)
                {
                        clear();

                        String[] name =
                        { test.get(test.size() - 1).getFname(),
                                        test.get(test.size() - 1).getLname() };
                        jtM.addRow(name);

                } else
                {
                        for (int i = 0; i < test.size(); i++)
                        {
                                String[] name =
                                { test.get(i).getFname(), test.get(i).getLname() };
                                jtM.addRow(name);
                        }
                }
        }

        //
        public void search()
        {
                try
                {
                        test.addAll(to.searchUser(Integer.parseInt(jtID.getText())));
                } catch (NumberFormatException nfe)
                {
                        test.addAll(to.searchUser(jtID.getText()));
                }
                fillTable();
                if (jtM.getRowCount() <= 0)
                        JOptionPane
                                        .showMessageDialog(null,
                                                        "User not found. Hint: Use a CAPITAL LETTER for the 1st Letter");
        }
}